﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.Collections.Generic;
using KPIS.GERP.GIMS.DAL;
using KPIS.GERP.GIMS.MODEL;

namespace KPIS.GERP.GIMS.APP.APP_UI.Edit_Form
{
    public partial class ICM_Inventory_Repair : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        D_Asset d_asset = new D_Asset();
        M_Asset m_asset = new M_Asset();
        D_JobSiteSeq d_job_site = new D_JobSiteSeq();
        M_JobSiteSeq m_job_site = new M_JobSiteSeq();
        D_Warehouse d_warehouse = new D_Warehouse();
        MasterWarehouseInfo m_warehouse = new MasterWarehouseInfo();
        D_Contact d_contact = new D_Contact();
        M_Contact m_contact = new M_Contact();
        D_IcmInventoryRepair d_inventory_repair = new D_IcmInventoryRepair();
        M_IcmInventoryRepair m_inventory_repair = new M_IcmInventoryRepair();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hstatus.Text = Request.QueryString["status"].ToString();
                EnableFalseOrganize();
                txtReceiveDate.Visible = false;
                lblReceiveDate.Visible = false;
                lbtReceiveDate.Visible = false;
                lblWarnReceiveDate.Visible = false;
                if (hstatus.Text == "Add")
                {
                    hRepairStatus.Text = "R";
                    txtSendRepairDateSystem.Text = cn.ConvertStrToStrDate(DateTime.Now.ToShortDateString());
                }
                if (hstatus.Text == "Edit")
                {
                    hrepairseq.Text = Request.QueryString["icm_repair_seq"].ToString();
                    LoadToObject();
                    
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ChkValidate())
            {
                try
                {
                    RadAjaxManager1.Alert("บันทึกข้อมูลเรียบร้อยแล้ว");
                    btnSave.Enabled = false;
                    m_inventory_repair.icm_repair_date = Convert.ToDateTime(txtSendRepairDateSystem.TextWithLiterals);
                    if (txtReceiveDate.Text != "")
                    {
                        m_inventory_repair.icm_receive_date = cn.ConvertNullDateTime(txtReceiveDate.TextWithLiterals);
                    }
                    m_inventory_repair.asset_code = cmbAssetCode.Text;
                    m_inventory_repair.icm_repair_descr = txtRepairDescription.Text;
                    m_inventory_repair.icm_cause = txtCauseRepair.Text;
                    m_inventory_repair.repair_amount = cn.ConvertNullDouble(txtAmountRepair.Text);
                    m_inventory_repair.repair_discount = cn.ConvertNullDouble(txtDiscount.Text);
                    m_inventory_repair.job_site_seq = cn.ConvertNullInt(cmbJobSite.SelectedValue);
                    m_inventory_repair.icm_wh_seq = cn.ConvertNullInt(cmbWarehouse.SelectedValue);
                    m_inventory_repair.icm_dealer_repair_seq = cn.ConvertNullInt(cmbDealerRepair.SelectedValue);
                    m_inventory_repair.icm_repair_remark = txtRemark.Text;
                    if (hRepairStatus.Text == "")
                        hRepairStatus.Text = "R";
                    m_inventory_repair.repair_status = hRepairStatus.Text;
                    m_inventory_repair.record_status = "U";
                    if (hstatus.Text == "Add")
                    {
                        m_inventory_repair.created_by = cn.UserID;
                        m_inventory_repair.created_when = DateTime.Now;
                        d_inventory_repair.Insert_Inventory_Repair(m_inventory_repair);
                    }
                    else if (hstatus.Text == "Edit")
                    {
                        m_inventory_repair.icm_repair_seq = Convert.ToInt32(hrepairseq.Text);
                        m_inventory_repair.updated_by = cn.UserID;
                        m_inventory_repair.updated_when = DateTime.Now;
                        d_inventory_repair.Update_Inventory_Repair(m_inventory_repair);
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    SetEnableObject(false);
                    RadAjaxManager1.ResponseScripts.Add(@"CloseWindow();");
                }
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (hstatus.Text == "Edit")
            {
                LoadToObject();
            }
            else if (hstatus.Text == "Add")
            {
                ClearObject();
            }
        }

        //============================ Set Object  ===========================
        protected void cmbJobSite_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<M_JobSiteSeq> infoList = d_job_site.GetListFilterByName(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbJobSite.Items.Add(new RadComboBoxItem(infoList[i].job_site_name, infoList[i].job_site_seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdCode = new TableCell();
                Label lblCode = new Label();
                lblCode.Text = infoList[i].job_site_ref;
                tdCode.Width = Unit.Pixel(200);
                tdCode.Controls.Add(lblCode);
                tr.Cells.Add(tdCode);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].job_site_name;
                tdName.Width = Unit.Pixel(200);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                ((Table)cmbJobSite.Items[j].FindControl("tableJoBSite")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbWarehouse_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<MasterWarehouseInfo> infoList = d_warehouse.GetListFillByName(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbWarehouse.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(200);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                TableCell tdDesc = new TableCell();
                Label lblDesc = new Label();
                lblDesc.Text = infoList[i].Descr;
                tdDesc.Width = Unit.Pixel(200);
                tdDesc.Controls.Add(lblDesc);
                tr.Cells.Add(tdDesc);
                ((Table)cmbWarehouse.Items[j].FindControl("tableWarehouse")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbAssetCode_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<M_Asset> infoList = d_asset.GetListFillByName(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbAssetCode.Items.Add(new RadComboBoxItem(infoList[i].asset_code, infoList[i].asset_code.ToString()));
                TableRow tr = new TableRow();
                TableCell tdCode = new TableCell();
                Label lblCode = new Label();
                lblCode.Text = infoList[i].asset_code;
                tdCode.Width = Unit.Pixel(200);
                tdCode.Controls.Add(lblCode);
                tr.Cells.Add(tdCode);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].asset_name;
                tdName.Width = Unit.Pixel(200);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                TableCell tdDescr = new TableCell();
                Label lblDescr = new Label();
                lblDescr.Text = infoList[i].asset_remark;
                tdDescr.Width = Unit.Pixel(200);
                tdDescr.Controls.Add(lblDescr);
                tr.Cells.Add(tdDescr);
                ((Table)cmbAssetCode.Items[j].FindControl("tableAssetCode")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbDealerRepair_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<M_Contact> infoList = d_contact.GetListFilterByName(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbDealerRepair.Items.Add(new RadComboBoxItem(infoList[i].icm_con_name_th, infoList[i].icm_con_seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdNameT = new TableCell();
                Label lblNameT = new Label();
                lblNameT.Text = infoList[i].icm_con_name_th;
                tdNameT.Width = Unit.Pixel(200);
                tdNameT.Controls.Add(lblNameT);
                tr.Cells.Add(tdNameT);
                TableCell tdNameE = new TableCell();
                Label lblNameE = new Label();
                lblNameE.Text = infoList[i].icm_con_name_eng;
                tdNameE.Width = Unit.Pixel(200);
                tdNameE.Controls.Add(lblNameE);
                tr.Cells.Add(tdNameE);
                ((Table)cmbDealerRepair.Items[j].FindControl("tableDealer")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        protected void rbJobSite_CheckedChanged(object sender, EventArgs e)
        {
            ChooseJoBSite(true);
            cmbWarehouse.SelectedValue = "";
            cmbWarehouse.Text = "";
        }
        protected void rbWarehouse_CheckedChanged(object sender, EventArgs e)
        {
            ChooseJoBSite(false);
            cmbJobSite.SelectedValue = "";
            cmbJobSite.Text = "";
        }
        protected void chkRepairStatus_CheckedChanged(object sender, EventArgs e)
        {
            txtReceiveDate.Visible = true;
            lblReceiveDate.Visible = true;
            lbtReceiveDate.Visible = true;
            lblWarnReceiveDate.Visible = true;
            btnSave.Enabled = true;
            if (chkRepairStatus.Checked == true)
            {
                hRepairStatus.Text = "A";
            }
            else
            {
                hRepairStatus.Text = "R";
            }
        }
        //============================ Set Property & Function  ===========================
        private bool ChkValidate()
        {
            bool pFlag = true;
            if (txtSendRepairDateSystem.Text == "")
            {
                pFlag = false;
                txtSendRepairDateSystem.Focus();
                RadAjaxManager1.Alert("กรุณากรอกวันที่ส่งซ่อม");
            }
            else if (cmbAssetCode.Text == "")
            {
                pFlag = false;
                cmbAssetCode.Focus();
                RadAjaxManager1.Alert("กรุณาเลือกรหัสพัสดุที่จะทำการส่งซ่อม");
            }
            else if (txtRepairDescription.Text == "")
            {
                pFlag = false;
                txtRepairDescription.Focus();
                RadAjaxManager1.Alert("กรุณากรอกรายการส่งซ่อม");
            }
            else if (txtCauseRepair.Text == "")
            {
                pFlag = false;
                txtCauseRepair.Focus();
                RadAjaxManager1.Alert("กรุณากรอกสาเหตุของการส่งซ่อม");
            }
            else if (cmbDealerRepair.Text == "")
            {
                pFlag = false;
                cmbDealerRepair.Focus();
                RadAjaxManager1.Alert("กรุณาเลือกบริษัทที่ซ่อมพัสดุ");
            }
            else if ((chkRepairStatus.Checked == true) & (txtReceiveDate.Text == ""))
            {
                pFlag = false;
                txtReceiveDate.Focus();
                RadAjaxManager1.Alert("กรุณากรอกวันที่รับเข้า");
            }
            return pFlag;
        }
        private void EnableFalseOrganize()
        {
            lblJoBSiteSendRepair.Visible = false;
            lblWarehouse.Visible = false;
            cmbJobSite.Visible = false;
            cmbWarehouse.Visible = false;
            lblWarningOrg.Visible = false;
        }
        private void ChooseJoBSite(bool pFlag)
        {
            lblJoBSiteSendRepair.Visible = pFlag;
            cmbJobSite.Visible = pFlag;
            lblWarehouse.Visible = !pFlag;
            cmbWarehouse.Visible = !pFlag;
            lblWarningOrg.Visible = true;
        }
        private void SetEnableObject(bool pFlag)
        {
            txtSendRepairDateSystem.Enabled = pFlag;
            cmbAssetCode.Enabled = pFlag;
            txtRepairDescription.Enabled = pFlag;
            txtCauseRepair.Enabled = pFlag;
            txtAmountRepair.Enabled = pFlag;
            txtDiscount.Enabled = pFlag;
            cmbJobSite.Enabled = pFlag;
            cmbWarehouse.Enabled = pFlag;
            cmbDealerRepair.Enabled = pFlag;
            txtRemark.Enabled = pFlag;
            rbWarehouse.Enabled = pFlag;
            rbJobSite.Enabled = pFlag;
            //btnSave.Enabled = pFlag;
            btnCancel.Enabled = pFlag;
        }
        private void ClearObject()
        {
            txtReceiveDate.Text = "";
            txtSendRepairDateSystem.Text = "";
            cmbAssetCode.ClearSelection();
            cmbAssetCode.Text = "";
            txtRepairDescription.Text = "";
            txtCauseRepair.Text = "";
            txtAmountRepair.Text = "";
            txtDiscount.Text = "";
            cmbJobSite.ClearSelection();
            cmbJobSite.Text = "";
            cmbWarehouse.ClearSelection();
            cmbWarehouse.Text = "";
            cmbDealerRepair.ClearSelection();
            cmbDealerRepair.Text = "";
            txtRemark.Text = "";
            chkRepairStatus.Checked = false;
            rbWarehouse.Checked = false;
            rbJobSite.Checked = false;
        }
        private void LoadToObject()
        {
            M_IcmInventoryRepair info = d_inventory_repair.GetListFillBySeq(cn.ConvertNullInt(hrepairseq.Text));
            txtReceiveDate.Text = cn.ConvertStrToStrDate(info.icm_receive_date.ToString());
            txtSendRepairDateSystem.Text = cn.ConvertStrToStrDate(info.icm_repair_date.ToString());
            cmbAssetCode.Text = info.asset_code.ToString();
            cmbAssetCode.SelectedValue = info.asset_code.ToString();
            txtRepairDescription.Text = info.icm_repair_descr.ToString();
            txtCauseRepair.Text = info.icm_cause.ToString();
            txtAmountRepair.Text = info.repair_amount.ToString();
            txtDiscount.Text = info.repair_discount.ToString();
            cmbJobSite.Text = info.job_site_name.ToString();
            cmbJobSite.SelectedValue = info.job_site_seq.ToString();
            cmbWarehouse.Text = info.warehouse_name.ToString();
            cmbWarehouse.SelectedValue = info.icm_wh_seq.ToString();
            cmbDealerRepair.Text = info.icm_dealer_repair_name.ToString();
            cmbDealerRepair.SelectedValue =  info.icm_dealer_repair_seq.ToString();
            txtRemark.Text = info.icm_repair_remark.ToString();
            txtSendRepairDateSystem.Enabled = false;
            cmbAssetCode.Enabled = false;
           // SetEnableObject(false);
            if (info.icm_wh_seq.ToString() != "")
            {
                rbWarehouse.Checked = true;
            }
            else if (info.job_site_seq.ToString() != "")
            {
                rbJobSite.Checked = true;
            }
        }


    }
}
